define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Industryrankingsdetail, $stateParams, $timeout, $window, $ionicPopover, GoAddress, GoCatTypeAuth, GoDialog, GoPage, GoShare, GoWapAuth,$ionicSideMenuDelegate) {
		$scope.input = new Object();
		$scope.input.region = {id:0,name:"请选择地区"}; //检索地域id
		$scope.input.cat1 = {id:0,name:"请选择类别"}; ; //检索分类id
		$scope.region_id = "";//地区id
		$scope.cat_id = "";//分类id
		$scope.title = "优秀旗舰店";
		//店铺类型
		//1=>制造商
		//2=>品牌运营商
		//3=>代理商
		//4=>经销商
		$scope.store_type = $stateParams.store_type; //店铺类型
		if($scope.store_type == 2) {
			$scope.title = "优秀品牌运营商";
		}
		if($scope.store_type == 3) {
			$scope.title = "优秀授权中心";
		}
		if($scope.store_type == 4) {
			$scope.title = "优秀加盟店";
		}

		/************其他：模态窗口*************/
		$ionicPopover.fromTemplateUrl('templates/modal/sort1.html', {
			scope: $scope
		}).then(function(popover) {
			$scope.popover = popover;
		});
		$scope.openSort = function($event) { //打开
			$scope.popover.show($event);
		};
		$scope.closeSort = function() {
			$scope.popover.hide();
		};
		//销毁目标
		$scope.$on('$destroy', function() {
			$scope.popover.remove();
		});
		/************其他：模态窗口*************/
		
		//页面离开，分享关闭
		$scope.$on('$ionicView.afterLeave', function() {
			$scope.closeGuidShare();
		}, false);
		
		//加载数据
		$scope.order = 1; //默认综合排序
		$scope.data_init = function() {
			if($scope.input.region&&$scope.input.region.id!=0){
				$scope.region_id = $scope.input.region.id;
			}else{
				$scope.region_id = "";
			}
			if($scope.input.cat1&&$scope.input.cat1.id!=0){
				$scope.cat_id = $scope.input.cat1.id;
			}else{
				$scope.cat_id = "";
			}
			Industryrankingsdetail.getlist($scope.region_id, $scope.cat_id, $scope.order, 1, $scope.store_type, function(data) {
				$scope.getlist = data.store;
				$scope.region = data.region;
				$scope.input.nPage = 1; //因为排序里用了$scope.data_init();所以也必须把页数的初始化放在这里
				$scope.go_share_wx();
			});
		}
		$scope.data_init();

		//综合排序
		$scope.orderBy_mix = function() {
			if($scope.order == 1) {
				$scope.order = 2;
			} else {
				$scope.order = 1;
			}
			$scope.data_init();
		}
		//代理商数量排序和经销商排序共用
		$scope.orderBy_agent = function() {
			if($scope.order == 9) {
				$scope.order = 10;
			} else {
				$scope.order = 9;
			}
			$scope.data_init();
		}
		//服务能力排序
		$scope.orderBy_service = function() {
			if($scope.order == 7) {
				$scope.order = 8;
			} else {
				$scope.order = 7;
			}
			$scope.data_init();
			$scope.closeSort();
		}
		//品牌数排序
		$scope.orderBy_brand = function() {
			if($scope.order == 11) {
				$scope.order = 12;
			} else {
				$scope.order = 11;
			}
			$scope.data_init();
			$scope.closeSort();
		}
		//投票数排序
		$scope.orderBy_vote = function() {
			if($scope.order == 13) {
				$scope.order = 14;
			} else {
				$scope.order = 13;
			}
			$scope.data_init();
			$scope.closeSort();
		}
		//经营规模排序
		$scope.orderBy_scale = function() {
			if($scope.order == 15) {
				$scope.order = 16;
			} else {
				$scope.order = 15;
			}
			$scope.data_init();
		}

		/***只需要类目一***/
		var items_cat = new Array();
		var obj1_cat = {
			name: 'input.cat1'
		};
		items_cat.push(obj1_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);
		$scope.doSelectProvince = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择地区", $scope.region, $scope.input.region, function (data) {
				$scope.input.region = data;
			});
		}
		$scope.doSelectCat1 = function () {
			//id是值 name是显示
			GoDialog.showSelect("类别", $scope.cat1_items, $scope.input.cat1, function (data) {
				$scope.input.cat1 = data;
			});
		}
		/***************刷新加载****************/
		$scope.input.nPage = 1;
		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Industryrankingsdetail.getlist($scope.region_id, $scope.cat_id, $scope.order, 1, $scope.store_type, function(data) {
					if(data.store != null) {
						$scope.getlist = data.store;
					} else {
						$scope.getlist = new Object();
					}
					$scope.input.nPage = 1;
				},
				function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Industryrankingsdetail.getlist($scope.region_id, $scope.cat_id, $scope.order, $scope.input.nPage + 1, $scope.store_type, function(data) {
					if(data.store != null) {
						if($scope.getlist != undefined) {
							$scope.getlist = $scope.getlist.concat(data.store);
							$scope.input.nPage += 1;
						} else {
							$scope.getlist = data.store;
						}
					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};
		/***************刷新加载****************/

		//投票--一天内不能重复投票==通过cookie在app不行
		$scope.vote = function(store_id,$event) {
			$event.stopPropagation();//阻止事件冒泡
			$scope.voteTime = localStorage.getItem('votedTime'); //获取投票时间
			$scope.nowTime = new Date().getTime(); //现在的时间戳:js的时间戳是毫秒
			if($scope.nowTime - $scope.voteTime >= 86400000) { //是否相差一天
				Industryrankingsdetail.get_vote(store_id, function(data) {
					if(data.success) {
						GoDialog.toast("投票成功！");
						localStorage.setItem('votedTime', $scope.nowTime); //在本地内存增加投票时的时间						
					} else {
						GoDialog.toast(data.msg);
					}
				});
			} else {
				GoDialog.toast("一天内不能重复投票！");
			}
		}

		//分享qq和微信。1==微信   3==qq
		$scope.params = new Object(); //全局
		$scope.go_share = function(index, arrIndex, $event) {
			$event.stopPropagation();//阻止事件冒泡
			//分享参数
			$scope.params.store_id = $scope.getlist[arrIndex].store_id;
			$scope.params.title = $scope.getlist[arrIndex].name; //标题 $scope.getlist
			$scope.params.desc = "未来万家 行业排名"; //简介
			$scope.params.thumb = $scope.getlist[arrIndex].store_pic; //图片
			//$scope.params.link = 'industryrankingsdetail/' + $scope.store_type;//之前的
			$scope.params.link = 'industryrankingsone/' + $scope.params.store_id;
			if($scope.openGuidShare()) {
				GoShare.do_share($scope.params, index);
			} else {
				$scope.go_share_wx();
			}
		}

		//分享（自定义每个页面的参数）
		$scope.go_share_wx = function() {
			//分享打开
			GoWapAuth.execisoff(false);
			if($scope.params.store_id == undefined) {
				//设置分享
				var param = new Object();
				param.title = "未来万家"; //标题
				param.desc = $scope.title; //简介
				param.thumb = undefined; //图片
				param.link = 'industryrankingsdetail/' + $scope.store_type;
				GoWapAuth.execshare(param);
			} else {
				//设置分享
				GoWapAuth.execshare($scope.params);
			}
		}
		
		//侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		
		//详情
		$scope.on_showDetail = function(store_id){
			GoPage.go_states("industryrankingsone",{'store_id':store_id})
		}
		
	}

	controller.$inject = ['$scope', 'Industryrankingsdetail', "$stateParams", '$timeout', '$window', '$ionicPopover', 'GoAddress', 'GoCatTypeAuth', 'GoDialog', 'GoPage', "GoShare", "GoWapAuth","$ionicSideMenuDelegate"];

	return app.registerController("industryrankingsdetailctrl", controller);
});